package com.example.com.skills_utf8;


import java.util.List;

import com.utils.TIME;
import com.utils.Utils1;
import com.utils.cHighTimer;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;




public class Sensor31Activity extends Activity
{
	
	Sensor31Activity activity = this;
	
	Sensor mAccSensor;
	private SensorManager sm;
	
	AccListener mAccListener = new AccListener();;
	
	TextView tv_x_acc, tv_y_acc, tv_z_acc;
	
//	TIME mTimeCounter;
	cHighTimer mTimer = new cHighTimer();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sensor31);
		
		
		sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		List<Sensor> SensorListData =sm.getSensorList(Sensor.TYPE_ALL);
		for(int i =0; i<SensorListData.size(); i++){
			Sensor s = SensorListData.get(i);
			System.out.println(""+s.getName());
		}
		
		//获取加速度传感器
		mAccSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		
		
		tv_x_acc = (TextView)findViewById(R.id.tv_x_acc);
		tv_y_acc = (TextView)findViewById(R.id.tv_y_acc);
		tv_z_acc = (TextView)findViewById(R.id.tv_z_acc);
	
		
		mTimer.start_counter();
		
		
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	//	mAccListener = new AccListener();
		sm.registerListener( mAccListener, mAccSensor, SensorManager.SENSOR_DELAY_GAME);
	}
	
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		sm.unregisterListener(mAccListener);
	}
	
	
	
	public void OnShake(){
		Utils1.showToastShort(activity, "摇一摇~");
	}
	
	
	
	
	class AccListener implements SensorEventListener
	{


		private float old_x_acc;
		private float old_y_acc;
		private float old_z_acc;

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub

			
		    
		    float elapsed =mTimer.get_elapsed_time();
		   if( mTimer.controlFPS_Return(elapsed, 30) == false) // 
			   return;
		    
		    
		    System.out.println("================="+elapsed);
		    
		    
		    
		    float x_acc = event.values[0];
		    float y_acc = event.values[1];
		    float z_acc = event.values[2];
		    
		    /*
		    System.out.println(""+x_acc);
		    System.out.println(""+y_acc);
		    System.out.println(""+z_acc);
		    */
		    
		    tv_x_acc.setText("x轴加速度: "+x_acc);
		    tv_y_acc.setText("y轴加速度: "+y_acc);
		    tv_z_acc.setText("z轴加速度: "+z_acc);
		    
		    
		    // 
		    
		    
		    if(z_acc < -8){
		    	Utils1.showToastShort(activity, "手机背面朝上~");
		    }
		    
		    
		    
		    
		    float delta_x_acc = x_acc - old_x_acc;
		    float delta_y_acc = y_acc - old_y_acc;
		    float delta_z_acc = z_acc - old_z_acc;
		    
		    
		    double delta = Math.sqrt( (double)(delta_x_acc*delta_x_acc + delta_y_acc*delta_y_acc + 
		    		delta_z_acc*delta_z_acc) );
		    
		    System.out.println("delta: "+delta);
		    
		    // 判断x,y方向加速度大于多少就ok了...
		    if(delta > 5)
		    	OnShake();
		    
		    
		    old_x_acc = x_acc;
		    old_y_acc = y_acc;
		    old_z_acc = z_acc;
		    
		    
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

			
		}
		
	};

	
	
	

	
};





//  

/*  各传感器
04-27 20:50:56.320: I/System.out(19549): LSM330DLC 3-axis Accelerometer
04-27 20:50:56.320: I/System.out(19549): AK8975C 3-axis Magnetic field sensor
04-27 20:50:56.320: I/System.out(19549): iNemoEngine Orientation sensor
04-27 20:50:56.320: I/System.out(19549): CM36651 Light sensor
04-27 20:50:56.320: I/System.out(19549): CM36651 Proximity sensor
04-27 20:50:56.320: I/System.out(19549): LSM330DLC Gyroscope sensor
04-27 20:50:56.320: I/System.out(19549): iNemoEngine Gravity sensor
04-27 20:50:56.320: I/System.out(19549): iNemoEngine Linear Acceleration sensor
04-27 20:50:56.320: I/System.out(19549): iNemoEngine Rotation_Vector sensor
04-27 20:50:56.320: I/System.out(19549): LPS331AP Pressure Sensor
04-27 20:50:56.320: I/System.out(19549): Rotation Vector Sensor
04-27 20:50:56.320: I/System.out(19549): Gravity Sensor
04-27 20:50:56.320: I/System.out(19549): Linear Acceleration Sensor
04-27 20:50:56.320: I/System.out(19549): Orientation Sensor
04-27 20:50:56.320: I/System.out(19549): Corrected Gyroscope Sensor
*/


